package com.tz.yiqingapp.controller.user;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tz.yiqingapp.bean.*;
import com.tz.yiqingapp.config.AccessLimit;
import com.tz.yiqingapp.dto.PageDTO;
import com.tz.yiqingapp.mapper.*;
import com.tz.yiqingapp.utils.MyPasswordEncoder;
import com.tz.yiqingapp.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserLogin {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    YimiaoOrderMapper yimiaoOrderMapper;
    @Autowired
    UserHealthMapper userHealthMapper;
    @Autowired
    HosptialMapper hosptialMapper;
    @Autowired
    UserHealthReportMapper userHealthReportMapper;

    @AccessLimit(times = 5)
    @PostMapping("/login")
    @ResponseBody
    public HashMap<String,Object> login (@RequestBody User user, HttpServletRequest request) {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getName,user.getName());
        List<User> users  = userMapper.selectList(userQueryWrapper);
        if(users.size()>0){
            if(MyPasswordEncoder.domatches(user.getPassword(),  users.get(0).getPassword())){
                request.getSession().setAttribute("loginrole","user");
                request.getSession().setAttribute("loginid",users.get(0).getId());
                System.out.println("sessionid :" + request.getSession().getId());
                return ResponseUtils.success("login success:" );
            }
            return ResponseUtils.fail("账户或密码错误！");
        }
        return ResponseUtils.fail("用户不存在");
    }

    @AccessLimit(times = 5)
    @PostMapping("/regist")
    @ResponseBody
    public HashMap<String,Object> regist (@RequestBody User user, HttpServletRequest request) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getName,user.getName());
        List<User> users  = userMapper.selectList(userQueryWrapper);
        if(users.size()>0){
            return ResponseUtils.fail("用户已存在！");
        }
        userMapper.insert(user);
        return ResponseUtils.success("用户创建成功！");
    }

    @AccessLimit(times = 5)
    @PostMapping("/getDashboardInfo")
    @ResponseBody
    public HashMap<String,Object> getDashboardInfo (@RequestBody User user, HttpServletRequest request) {

        System.out.println("sessionid :" + request.getSession().getId());
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("user")){
            return ResponseUtils.notlogin("not login");
        }

        HashMap<String ,Object> hashMap = new HashMap<>();
        hashMap.put("code",1);
        return hashMap;

    }
    @AccessLimit(times = 5)
    @PostMapping("/logout")
    public HashMap<String,Object> logout (@RequestBody User user, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("user")) {
            request.getSession().setAttribute("loginrole",null);
            return ResponseUtils.fail("login out success ");
        }
        return ResponseUtils.notlogin("login fail ");
    }
    @AccessLimit(times = 5)
    @PostMapping("/changepass")
    public HashMap<String,Object> changepass (@RequestBody  User user, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("user")){
            long uid = (long) request.getSession().getAttribute("loginid");

            MyPasswordEncoder passwordEncoder = new MyPasswordEncoder();
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            user.setId(uid);
            if(userMapper.updateById(user) >0){
                return ResponseUtils.success("change success");
            }
        }
        return ResponseUtils.notlogin("login fail");
    }
    @AccessLimit(times = 5)
    @PostMapping("/updateMyInfo")
    public HashMap<String,Object> updateMyInfo (@RequestBody User user, HttpServletRequest request) {
        if (request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("user")) {
            long uid = (long) request.getSession().getAttribute("loginid");
//            MyPasswordEncoder passwordEncoder = new MyPasswordEncoder();
//            user.setPassword(passwordEncoder.encode(user.getPassword()));
            User lastuser = userMapper.selectById(uid);
            user.setPassword(lastuser.getPassword());
            user.setId(uid);
            user.setInfoed(true);
            if (userMapper.updateById(user) > 0) {
                return ResponseUtils.success("change success");
            }
        }
        return ResponseUtils.notlogin("login fail");
    }



    @AccessLimit(times = 5)
    @PostMapping("/getUserBaseInfo")
    @ResponseBody
    public HashMap<String, Object> getUserBaseInfo(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("user")){
            return ResponseUtils.notlogin("not login");
        }
        long uid = (long)request.getSession().getAttribute("loginid");
        User user = userMapper.selectById(uid);
        UserBaseInfo userBaseInfo = new UserBaseInfo(user);
        QueryWrapper<UserHealth> userHealthQueryWrapper = new QueryWrapper<>();
        userHealthQueryWrapper.lambda().eq(UserHealth::getUid,uid);
        List<UserHealth> userHealths = userHealthMapper.selectList(userHealthQueryWrapper);
        if(userHealths.size()>0)
            userBaseInfo.setUserHealth(userHealths.get(0));
        QueryWrapper<UserHealthReport> userHealthReportQueryWrapper = new QueryWrapper<>();
        userHealthReportQueryWrapper.lambda().eq(UserHealthReport::getUid,uid);
        List<UserHealthReport> userHealthReportList = userHealthReportMapper.selectList(userHealthReportQueryWrapper);
        if(userHealthReportList.size()>0)
            userBaseInfo.setUserHealthReport(userHealthReportList.get(0));
        QueryWrapper<YimiaoOrder> yimiaoOrderQueryWrapper = new QueryWrapper<>();
        yimiaoOrderQueryWrapper.lambda().eq(YimiaoOrder::getUid,uid);
        List<YimiaoOrder> yimiaoOrders = yimiaoOrderMapper.selectList(yimiaoOrderQueryWrapper);
        if(yimiaoOrders.size()>0){
            YimiaoOrderInfo yimiaoOrderInfo = new YimiaoOrderInfo(yimiaoOrders.get(0));
            yimiaoOrderInfo.setHosptial(hosptialMapper.selectById(yimiaoOrders.get(0).getId()));
            yimiaoOrderInfo.setUser(userMapper.selectById(yimiaoOrders.get(0).getUid()));
            userBaseInfo.setYimiaoOrderInfo(yimiaoOrderInfo);
        }
        return ResponseUtils.success(userBaseInfo);
    }
}
